// pages/mes/issue-service/issue-service.js
const app = getApp();
import Toast from '../../../component/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        unitAmount: [],
        index: 0,
        serverItemTypes: [],
        amountValue: '',
        titleValue: '',
        contentValue: '',
        parentId: '',
        parentTypeName: '',
        unitList: [],
        nickName: '',
        avatarUrl: '',
        titleTips:'点击编辑标题',
        desTips:'点击编辑内容'
    },
    bindPickerChange(e) {
        // console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            index: e.detail.value
        })
    },
    preview() {
        let amount = this.data.amountValue;
        let title = this.data.titleValue;
        let content = this.data.contentValue;
        let unit = this.data.unitAmount[this.data.index];
        let id = this.data.parentId;
        let typeName = this.data.parentTypeName;
        let regAmount = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
        if (!amount) {
            Toast("请输入发布金额");
            return;
        }
        if (!regAmount.test(amount)) {
            Toast("请输入正确的金额");
            return;
        }
        if(Number.parseFloat(amount) <= 0){
            Toast("发布的金额不能小于0");
            return;
        }
        if (!title) {
            Toast("请输入标题");
            return;
        }
        if (!content) {
            Toast("请输入内容");
            return;
        }
        let obj = {
            amount: amount,
            title: title,
            unit: unit,
            parentId: id,
            parentTypeName: typeName,
            typeData: this.data.serverItemTypes,
            unitDes: this.data.unitList[this.data.index],
            nickName: this.data.nickName,
            avatarUrl: this.data.avatarUrl
        };
        let str = JSON.stringify(obj).replace(/&nbsp;/g, ' ');
        app.globalData.serviceContent = content;
        wx.navigateTo({
            url: '/pages/mes/issue-preview/issue-preview?obj=' + JSON.stringify(obj)
        })
    },
    amountInput(event) {
        this.setData({
            amountValue: event.detail.value
        });
    },
    titleInput(event) {
        this.setData({
            titleValue: event.detail.value
        });
    },
    contentInput(event) {
        this.setData({
            contentValue: event.detail.value
        });
    },
    unitDesClick() {
        wx.showModal({
            title: '价格服务说明',
            content: this.data.unitList[this.data.index].UnitDes,
            showCancel: false,
            confirmColor: '#E71015'
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let types = app.globalData.serverItemTypes;
        let obj = JSON.parse(options.obj);
        console.log("issue-service",obj);
        let unitAmount = [];
        obj.unitList.forEach(function(item) {
            unitAmount.push(item.UnitName);
        });
        this.setData({
            serverItemTypes: types,
            parentId: obj.id,
            parentTypeName: obj.typeName,
            unitList: obj.unitList,
            unitAmount: unitAmount,
            nickName: obj.nickName,
            avatarUrl: obj.avatarUrl,
            titleTips: obj.titleTips,
            desTips: obj.desTips
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})